package com.lyt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyt.reggie.common.R;
import com.lyt.reggie.entity.Employee;
import com.lyt.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * Author:李雨涛
 * Date:2023-02-23-17:12
 * 描述：
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1:MD5加密
        String password= employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名查数据是否存在
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<Employee>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //判断是否查询到数据
        if (emp==null){
            return R.error("登录失败");
        }
        //判断密码是否一致
        if(!emp.getPassword().equals(password)){
           return R.error("登录失败");
        }
        //判断账号是否被禁用
        if(emp.getStatus()==0){
            return R.error("账号已被禁用");
        }
        //登录成功，将员工id存储到session中
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);
    }
    //员工退出登录
   @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        //设置初始密码123456，进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //创建数据库表中一些字段的值（实体类中没有的）
       // employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获取当前用户的id,为Long型
       // Long empId=(Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        employeeService.save(employee);

        return R.success("新增员工成功");

    }
    //员工分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //创建分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构造条件构造器（条件查询）
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<Employee>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //service调用page
        employeeService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }
    //启用和禁用员工账号
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //log.info(employee.toString());
       // Long empId=(Long)request.getSession().getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
       // employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");

    }
    //根据id查找员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
       Employee employee= employeeService.getById(id);
       if(employee!=null){
           return R.success(employee);
       }
        return R.error("没有查询到对应的员工信息");
    }

}
